//
//  SYDesignerViewController.m
//  Term Of Section B
//
//  Created by 韩震明 on 15/11/27.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYDesignerViewController.h"
#import "SYDesignerViewController.h"
#import "SYDesignerTableViewCell.h"
#import "SYDesignerCollectionViewCell.h"
#import "SYSecondTableViewCell.h"
#import "SYCommodityViewController.h"
#import "BuyerCollectionViewCell.h"
#import "ManualCollectionViewCell.h"
#import "IndependentCollectionViewCell.h"
#import "HeaderCollectionViewCell.h"

#define SWIDTH [UIScreen mainScreen].bounds.size.width / 375.0
#define SHEIGHT [UIScreen mainScreen].bounds.size.height / 667.0


@interface SYDesignerViewController ()<IndependentCollectionViewCellDelegate, ManualCollectionViewCellDelegate, BuyerCollectionViewCellDelegate>

@property(nonatomic, retain)NSArray *arrCollectionArr;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)NSString *strID;
@property(nonatomic, strong)SYBaseTableView *myTableView;
@property(nonatomic, strong)SYBaseCollectionView *myCollectionView;
@property(nonatomic, strong)SYBaseCollectionView *headCollectionView;
@property(nonatomic, strong)NSIndexPath *index;
@property(nonatomic, strong)UIView *headView;
@property(nonatomic, assign)NSInteger sa;
@property(nonatomic, strong)UICollectionView *collectionView;
@end

@implementation SYDesignerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:0.918 green:0.918 blue:0.918 alpha:1.00];
    
    // 关闭scrollview的自动适应
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.index = [NSIndexPath indexPathForRow:0 inSection:0];
#pragma mark collectionView
    
    self.arrCollectionArr = [NSArray array];
    self.arrCollectionArr = @[@"独立设计师", @"手工匠人", @"达人买手"];
    
    // 大得
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
    
    self.headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.headView];
    
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(self.view);
        HEIGHTToNum(40);
    }];
    
    // 小得
    SYBaseCollectionViewFlowLayout *heightFlow = [[SYBaseCollectionViewFlowLayout alloc] initWithItemSize:CGSizeMake([UIScreen mainScreen].bounds.size.width / 3, 40) scrollDirection:UICollectionViewScrollDirectionHorizontal sectionInsert:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    heightFlow.minimumLineSpacing = 0;
    
    self.headCollectionView = [[SYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 5, 375, 30) collectionViewLayout:heightFlow backgroundColor:[UIColor clearColor] superView:self.headView showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO delegate:self];
    
    [self.headCollectionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.headView);
        make.top.equalTo(self.headView.top).with.offset(5);
        make.bottom.equalTo(self.headView.bottom).with.offset(-5);
        
    }];
    
    
    [self.headCollectionView registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:@"shang"];
    
    SYBaseCollectionViewFlowLayout *flow = [[SYBaseCollectionViewFlowLayout alloc] initWithItemSize:[UIScreen mainScreen].bounds.size scrollDirection:(UICollectionViewScrollDirectionHorizontal) sectionInsert:UIEdgeInsetsMake(0, 0, 0, 0)];
    flow.minimumLineSpacing = 0;
    
    
     self.myCollectionView = [[SYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:flow backgroundColor:[UIColor clearColor] superView:self.view showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO delegate:self];
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headView.bottom).with.offset(0);
        make.left.right.bottom.equalTo(self.view);
        
    }];
    
    
//    [self.headView makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.left.top.bottom.equalTo(self.view);
//
//    }];
    
    
    
    self.myCollectionView.contentOffset = CGPointMake(0, 0);
    self.myCollectionView.pagingEnabled = YES;
    
    [self.myCollectionView registerClass:[IndependentCollectionViewCell class] forCellWithReuseIdentifier:@"naoting"];
    [self.myCollectionView registerClass:[ManualCollectionViewCell class] forCellWithReuseIdentifier:@"zhennaoting"];
    [self.myCollectionView registerClass:[BuyerCollectionViewCell class] forCellWithReuseIdentifier:@"tainaotingle"];
   

}

// 联动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.myCollectionView) {
        self.sa = self.myCollectionView.contentOffset.x / 375;
        
        for (NSInteger i = 0; i < 6; i++) {
            
            if (self.myCollectionView.contentOffset.x == 375 * i) {
                self.headCollectionView.contentOffset = CGPointMake(0, 0);
                self.index = [NSIndexPath indexPathForItem:i inSection:0];
            }
        }
    }
    [self.headCollectionView reloadData];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.headCollectionView) {
        return 3;
    } else {
        return 3;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.headCollectionView) {
        HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shang" forIndexPath:indexPath];
        cell.longLabel.hidden = YES;
        
        for (NSInteger i = 0; i < self.arrCollectionArr.count; i++) {
            cell.heightLabel.text = self.arrCollectionArr[indexPath.row];
            
//            NSLog(@"%@", self.arrCollectionArr[i]);
            if (indexPath.row == self.sa) {
                cell.longLabel.hidden = NO;
            }
        }
        
        // 点击变颜色
        if (self.index == indexPath ) {
            cell.heightLabel.textColor = [UIColor colorWithRed:0.816 green:0.608 blue:0.259 alpha:1.00];
        }  else {
            cell.heightLabel.textColor = [UIColor blackColor];
            
        }
        return cell;
    } else {
        
        if (indexPath.row == 0) {
            IndependentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"naoting" forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor grayColor];
            NSLog(@"1");
            
            cell.delegate = self;
            
            return cell;
            
        } else if (indexPath.row == 1) {
            ManualCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zhennaoting" forIndexPath:indexPath];
           
            
            cell.delegate = self;
            return cell;
        } else  {
            BuyerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tainaotingle" forIndexPath:indexPath];
            
            cell.delegate = self;
            return cell;
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ((collectionView = self.headCollectionView)) {
        self.sa = indexPath.row;
        self.myCollectionView.contentOffset = CGPointMake(self.sa * 375, 0);
        self.index = indexPath;
        [self.headCollectionView reloadData];
    } else {
        
        
        
    }
}

// 跳到商品页///////////



- (void)push:(NSString *)pushId {
    
    SYCommodityViewController *comm = [[SYCommodityViewController alloc] init];
    [self.navigationController pushViewController:comm animated:YES];
    
    comm.tempID = pushId;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
